package team.seekers.hula.question.questionDevelop.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.question.questionDevelop.entity.dto.QuestionDevelopCreateDTO;
import team.seekers.hula.question.questionDevelop.entity.dao.QuestionDevelopDO;
import team.seekers.hula.question.questionDevelop.entity.vo.QuestionDevelopListVO;
import team.seekers.hula.question.questionDevelop.entity.dto.QuestionDevelopUpdateDTO;
import team.seekers.hula.question.questionDevelop.mapper.QuestionDevelopMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.hula.user.user.entity.dto.UserInfoDTO;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class QuestionDevelopService extends ServiceImpl<QuestionDevelopMapper, QuestionDevelopDO> {


    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    public Map<String,List<QuestionDevelopListVO>> listOrderByWind(String questionId, String userId) {
        List<QuestionDevelopDO> list = lambdaQuery()
                .select()
                .eq(QuestionDevelopDO::getQuestionId, questionId)
                .orderByDesc(QuestionDevelopDO::getWind)
                .list();

        ArrayList<QuestionDevelopListVO> books = new ArrayList<>();
        ArrayList<QuestionDevelopListVO> movies = new ArrayList<>();
        ArrayList<QuestionDevelopListVO> articles = new ArrayList<>();

        ArrayList<String> userIds = new ArrayList<>();
        for (QuestionDevelopDO questionDevelopDO : list) {
            userIds.add(questionDevelopDO.getUserId());
        }
        Map<String, UserInfoDTO> userInfoDTOMap = userService.userIdsToUserInfoDTOs(userIds);

        for (QuestionDevelopDO questionDevelopDO : list) {
            QuestionDevelopListVO questionDevelopListVO = new QuestionDevelopListVO();
            BeanUtils.copyProperties(questionDevelopDO,questionDevelopListVO);
            questionDevelopListVO.setNickname(userInfoDTOMap.get(questionDevelopDO.getUserId()).getNickname());
            switch (questionDevelopDO.getType()){
                case 0:
                    books.add(questionDevelopListVO);
                    break;
                case 1:
                    movies.add(questionDevelopListVO);
                    break;
                case 2:
                    articles.add(questionDevelopListVO);
                    break;
            }
        }

        HashMap<String, List<QuestionDevelopListVO>> map = new HashMap<>();
        map.put("books",books);
        map.put("movies",movies);
        map.put("articles",articles);
        return map;
    }

    public String add(QuestionDevelopCreateDTO createDTO) {
        QuestionDevelopDO questionDevelopDO = new QuestionDevelopDO();
        BeanUtils.copyProperties(createDTO,questionDevelopDO);
        save(questionDevelopDO);
        return questionDevelopDO.getId();
    }

    public void delete(String id) {
        // 判断是不是该用户的
        QuestionDevelopDO questionDevelopDO = getById(id);
        if(!MindUtils.isAdmin()){
            if(!questionDevelopDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }

        // 删除
        removeById(id);
    }

    public void update(QuestionDevelopUpdateDTO updateVO) {
        // 判断是不是该用户的评论
        QuestionDevelopDO questionDevelopDO = getById(updateVO.getId());
        if(!MindUtils.isAdmin()){
            if(!questionDevelopDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }
        //更新评论
        BeanUtils.copyProperties(updateVO, questionDevelopDO);
        updateById(questionDevelopDO);
    }

    public void addGood(String id) {
        QuestionDevelopDO questionDevelopDO = getById(id);
        questionDevelopDO.setWind(questionDevelopDO.getWind()+1);
        updateById(questionDevelopDO);
    }

    public void reduceGood(String id) {
        QuestionDevelopDO questionDevelopDO = getById(id);
        questionDevelopDO.setWind(questionDevelopDO.getWind()-1);
        updateById(questionDevelopDO);
    }

}
